package com.jcl.recruit.resume.mapper;

import com.jcl.recruit.mapper.SqlMapper;
import com.jcl.recruit.mapper.provider.SqlProvider;
import com.jcl.recruit.resume.model.ResumeWork;
import com.jcl.recruit.resume.provider.ResumeWorkSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


public interface ResumeWorkMapper extends SqlMapper {

    String TABLE_NAME = "resume_work";

    String SQL_INSERT_SUFFIX_ENTITY = " (resume_id,corp_code,company,duty,position,start_date,end_date) ";

    String SQL_INSERT_SUFFIX_ENTITY_VALUES = " (#{entity.resumeId},#{entity.corpCode},#{entity.company},#{entity.duty}," +
            "#{entity.position},#{entity.startDate},#{entity.endDate}) ";

    String SQL_ORDER_START_DATE_DESC = " order by start_date desc ";

    @Select({

            QUERY_SCRIPT_START,
            SQL_QUERY_RESUME_ID,
            SQL_IN,
            FOREACH_START_LIST_RESUME_IDS,
            SQL_PROPERTY_RESUME_ID_VALUE,
            FOREACH_END,
            SQL_AND,
            SQL_CORP_CODE,
            SQL_ORDER_START_DATE_DESC,
            QUERY_SCRIPT_END
    })
    List<ResumeWork> findListByResumeIds(@Param("corpCode") String corpCode,
                                         @Param("resumeIds") List<Integer> resumeIds);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_RESUME_ID,
            SQL_EQUAL,
            SQL_PROPERTY_RESUME_ID_VALUE,
            SQL_AND,
            SQL_CORP_CODE,
            SQL_ORDER_START_DATE_DESC,
            QUERY_SCRIPT_END
    })
    List<ResumeWork> findListByResumeId(@Param("corpCode") String corpCode,
                                        @Param("resumeId") Integer resumeId);


    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_RESUME_ID,
            SQL_EQUAL,
            SQL_PROPERTY_RESUME_ID_VALUE,
            SQL_AND,
            SQL_CORP_CODE,
            SQL_ORDER_START_DATE_DESC,
            QUERY_SCRIPT_END
    })
    List<Map<String, Object>> findObjListByResumeId(@Param("corpCode") String corpCode,
                                                    @Param("resumeId") Integer resumeId);

    @Select({
            SQL_COUNT_ENTITY,
            " corp_code = #{entity.corpCode} ",
            SQL_AND,
            " company = #{entity.company} ",
            SQL_AND,
            " position = #{entity.position} ",
            SQL_AND,
            " duty = #{entity.duty} ",
            SQL_AND,
            " start_date = #{entity.startDate} ",
            SQL_AND,
            " end_date = #{entity.endDate} ",
            SQL_AND,
            "resume_id = #{entity.resumeId}"
    })
    int isExitEntity(@Param("entity") ResumeWork entity);

    @Insert({
            QUERY_SCRIPT_START,
            SQL_INSERT,
            SQL_INSERT_VALUES,
            FOREACH_START_LIST_ENTITY_NO_OPEN,
            SQL_INSERT_SUFFIX_ENTITY_VALUES,
            FOREACH_END,
            QUERY_SCRIPT_END
    })
    int batchInsert(@Param("list") List<ResumeWork> list);

    @InsertProvider(type = ResumeWorkSqlProvider.class, method = SqlProvider.SQL_METHOD_INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ResumeWork entity);

    @UpdateProvider(type = ResumeWorkSqlProvider.class, method = SqlProvider.SQL_METHOD_UPDATE)
    int updateByPrimaryKey(ResumeWork entity);

    @Delete({
            SQL_DELETE_ID
    })
    int deleteById(@Param("id") Integer id);

    @Delete({
            QUERY_SCRIPT_START,
            SQL_DELETE_RESUME_ID,
            SQL_IN,
            FOREACH_START_LIST_RESUME_IDS,
            SQL_PROPERTY_RESUME_ID_VALUE,
            FOREACH_END,
            SQL_AND,
            SQL_CORP_CODE,
            QUERY_SCRIPT_END
    })
    int batchDeleteByResumeIds(@Param("corpCode") String corpCode,
                               @Param("resumeIds") List<Integer> resumeIds);

    String SQL_INSERT = "insert into " + TABLE_NAME + SQL_INSERT_SUFFIX_ENTITY;
    String SQL_QUERY_RESUME_ID = "select * from " + TABLE_NAME + " where resume_id ";
    String SQL_DELETE_RESUME_ID = "delete from " + TABLE_NAME + " where resume_id ";
    String SQL_DELETE_ID = "delete from " + TABLE_NAME + " where id = #{id}";
    String SQL_COUNT_ENTITY = "select count(*) from " + TABLE_NAME + " where ";

}
